package com.qsh.ts.mem;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by sehuaqi on 2017/6/13.
 * 定义了一个 RestController 处理器，用于接受hello请求信息，直接返回一个字符串。
 */
@RestController
public class MemController {

    private Logger logger = LoggerFactory.getLogger(MemController.class);

    @Autowired
    private MemDao memDao;

    @Value("${server.port}")
    String port;

    @RequestMapping("hello")
    public String hello(@RequestParam String name){
        logger.info(">>>>>处理器，用于接受hello请求信息，直接返回一个字符串");
        return "端口：" + port + "返回信息--->你好，" + name;
    }

    @RequestMapping("/login/{id}")
    public String hello(@PathVariable int id){
        logger.info(">>>>>处理器，用于接受hello请求信息，直接返回一个字符串");

        Map map = memDao.getUser(id);

        String nickename = "匿名用户";
        if(map !=null ){
            nickename = map.get("nick_name").toString();
        }

        return "你好，" + nickename + "，欢迎登陆" + port;
    }

    @RequestMapping("/timer/{id}")
    public List timer(@PathVariable int id){

        List list = memDao.getTimer(id);

        return list;
    }

    @RequestMapping("/addTimer/{t}/{c}")
    public String addTimer(@PathVariable String t,@PathVariable String c){
        String result = "exception";

        int r = memDao.addTimer(t,c);

        if(r > 0){
            result = "ok";
        }

        return result;
    }
}
